﻿
// LiWeiDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "LiWi.h"
#include "LiWiDlg.h"
#include "afxdialogex.h"
#include "Data.h"
#include "liwishell.h"
#pragma comment(lib,"liwishell.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#endif



// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

	// 实现
protected:
	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL OnInitDialog();
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CLiWiDlg 对话框



CLiWiDlg::CLiWiDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_LIWEI_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}



void CLiWiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB1, tabItem);
}

BEGIN_MESSAGE_MAP(CLiWiDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, &CLiWiDlg::OnBnClickedOk)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CLiWiDlg::OnTcnSelchangeTab1)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_Stop, &CLiWiDlg::OnBnClickedStop)
	ON_BN_CLICKED(IDC_Clear, &CLiWiDlg::OnBnClickedClear)
END_MESSAGE_MAP()


// CLiWiDlg 消息处理程序

BOOL CLiWiDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}


	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	CRect tabRect;
	tabItem.GetClientRect(&tabRect);
	tabRect.DeflateRect(2, 30, 5, 5);
	//子窗口名
	tabItem.InsertItem(0, L"连接");
	tabItem.InsertItem(1, L"直线插补");
	tabItem.InsertItem(2, L"圆弧插补");
	tabItem.InsertItem(3, L"连续插补");
	tabItem.InsertItem(4, L"螺旋插补");
	tabItem.InsertItem(5, L"IO控制");
	tabItem.InsertItem(6, L"回原点");
	tabItem.InsertItem(7, L"手轮测试");
	tabItem.InsertItem(8, L"报警处理");
	tabItem.InsertItem(9, L"电子凸轮跟随");
	tabItem.InsertItem(10, L"自动电子凸轮");
	tabItem.InsertItem(11, L"位置比较输出与锁存");
	tabItem.InsertItem(12, L"上位机与Basic交互");
	tabItem.InsertItem(13, L"自定义指令封装");


	//New窗口对象
	connect = new Connect;
	moveLine = new MoveLine;
	moveCircle = new MoveCircle;
	moveContinue = new MoveContinue;
	helical = new Helical;
	io = new IO;
	goHome = new GoHome;
	handWheel = new HandWheel;
	warn = new Warn;
	canWheel = new CanWheel;
	autoCam = new AutoCam;
	hwPs_Regist = new HwPs_Regist;
	pcAndBasic = new PCAndBasic;
	diyToDo = new DiyToDo;


	//连接窗口
	connect->Create(IDD_Connect, &tabItem);
	moveLine->Create(IDD_MoveLine, &tabItem);
	moveCircle->Create(IDD_MoveCircle, &tabItem);
	moveContinue->Create(IDD_MoveContinue, &tabItem);
	helical->Create(IDD_Helical, &tabItem);
	io->Create(IDD_IO, &tabItem);
	goHome->Create(IDD_GoHome, &tabItem);
	handWheel->Create(IDD_HandWheel, &tabItem);
	warn->Create(IDD_Warn, &tabItem);
	canWheel->Create(IDD_CamWheel, &tabItem);
	autoCam->Create(IDD_AutoCam, &tabItem);
	hwPs_Regist->Create(IDD_HwPs_Regist, &tabItem);
	pcAndBasic->Create(IDD_PCAndBasic, &tabItem);
	diyToDo->Create(IDD_DiyToDo, &tabItem);


	connect->MoveWindow(tabRect);
	moveLine->MoveWindow(tabRect);
	moveCircle->MoveWindow(tabRect);
	moveContinue->MoveWindow(tabRect);
	helical->MoveWindow(tabRect);
	io->MoveWindow(tabRect);
	goHome->MoveWindow(tabRect);
	handWheel->MoveWindow(tabRect);
	warn->MoveWindow(tabRect);
	canWheel->MoveWindow(tabRect);
	autoCam->MoveWindow(tabRect);
	hwPs_Regist->MoveWindow(tabRect);
	pcAndBasic->MoveWindow(tabRect);
	diyToDo->MoveWindow(tabRect);


	connect->ShowWindow(SW_SHOW);
	moveLine->ShowWindow(SW_HIDE);
	moveCircle->ShowWindow(SW_HIDE);
	moveContinue->ShowWindow(SW_HIDE);
	helical->ShowWindow(SW_HIDE);
	io->ShowWindow(SW_HIDE);
	goHome->ShowWindow(SW_HIDE);
	handWheel->ShowWindow(SW_HIDE);
	warn->ShowWindow(SW_HIDE);
	canWheel->ShowWindow(SW_HIDE);
	autoCam->ShowWindow(SW_HIDE);
	hwPs_Regist->ShowWindow(SW_HIDE);
	pcAndBasic->ShowWindow(SW_HIDE);
	diyToDo->ShowWindow(SW_HIDE);

	IniFlag();

	//计时器
	SetTimer(1, 200, NULL);



	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CLiWiDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CLiWiDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CLiWiDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CLiWiDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}

///// <summary>
///// 获取窗口位置坐标
///// </summary>
///// <returns></returns>
//BOOL CLiWiDlg::OnInitDialog()
//{
//	CLiWiDlg::OnInitDialog();
//
//	//TODO: 在此处添加额外的初始化
//	CRect rect;
//	GetClientRect(&rect);	//取客户区大小
//	old.x = rect.right - rect.left;
//}

void CLiWiDlg::OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	if (Data::CardIsConnect())
	{
		IniFlag();

		connect->ShowWindow(SW_HIDE);
		moveLine->ShowWindow(SW_HIDE);
		moveCircle->ShowWindow(SW_HIDE);
		moveContinue->ShowWindow(SW_HIDE);
		helical->ShowWindow(SW_HIDE);
		io->ShowWindow(SW_HIDE);
		goHome->ShowWindow(SW_HIDE);
		handWheel->ShowWindow(SW_HIDE);
		warn->ShowWindow(SW_HIDE);
		canWheel->ShowWindow(SW_HIDE);
		autoCam->ShowWindow(SW_HIDE);
		hwPs_Regist->ShowWindow(SW_HIDE);
		pcAndBasic->ShowWindow(SW_HIDE);
		diyToDo->ShowWindow(SW_HIDE);

		switch (tabItem.GetCurSel())
		{
		case 0:
			connect->ShowWindow(SW_SHOW);
			break;
		case 1:
			Data::moveLineFlag = true;
			moveLine->ShowWindow(SW_SHOW);
			break;
		case 2:
			Data::moveCircleFlag = true;
			moveCircle->ShowWindow(SW_SHOW);
			break;
		case 3:
			Data::moveContinueFlag = true;
			moveContinue->ShowWindow(SW_SHOW);
			break;
		case 4:
			Data::helicalFlag = true;
			helical->ShowWindow(SW_SHOW);
			break;
		case 5:
			Data::ioFlag = true;
			io->ShowWindow(SW_SHOW);
			break;
		case 6:
			Data::goHomeFlag = true;
			goHome->ShowWindow(SW_SHOW);
			break;
		case 7:
			Data::handWheelFlag = true;
			handWheel->ShowWindow(SW_SHOW);
			break;
		case 8:
			Data::warnFlag = true;
			warn->ShowWindow(SW_SHOW);
			break;
		case 9:
			Data::canWheelFlag = true;
			canWheel->ShowWindow(SW_SHOW);
			break;
		case 10:
			Data::autoCamFlag = true;
			autoCam->ShowWindow(SW_SHOW);
			break;
		case 11:
			Data::hwPs_RegistFlag = true;
			hwPs_Regist->ShowWindow(SW_SHOW);
			break;
		case 12:
			Data::pcAndBasicFlag = true;
			pcAndBasic->ShowWindow(SW_SHOW);
			break;
		case 13:
			Data::diyToDoFlag = true;
			diyToDo->ShowWindow(SW_SHOW);
			break;

		default:
			break;
		}
	}
	else {
		MessageBox(TEXT("请先连接控制器！！！！"));
	}
	//重绘窗口
	//CRect rect;
	//GetDlgItem(IDC_TAB1)->GetClientRect(&rect);
	//InvalidateRect(&rect, TRUE);
	//RedrawWindow();
	*pResult = 0;
}



//初始化标志
void CLiWiDlg::IniFlag()
{
	Data::moveLineFlag = false;
	Data::moveCircleFlag = false;
	Data::moveContinueFlag = false;
	Data::helicalFlag = false;
	Data::ioFlag = false;
}



/// <summary>
/// 定时器触发
/// </summary>
/// <param name="nIDEvent"></param>
void CLiWiDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (Data::CardIsConnect())
	{
		float dpos = 0;
		float speed0 = 0;
		int axisStop[5] = { 0,0,0,0,0 };
		CString strDPos;
		CString strSpeed0;
		for (size_t i = 0; i < 5; i++)
		{
			PMC_GetDpos(Data::handle, i, &dpos);
			strDPos.Format(_T("%.3f"), dpos);
			switch (i)
			{
			case 0:
				PMC_GetSpeed(Data::handle, 0, &speed0);
				strSpeed0.Format(_T("%.2f"), speed0);
				GetDlgItem(IDC_AxisPos0)->SetWindowTextW(strDPos);
				break;
			case 1:
				GetDlgItem(IDC_AxisPos1)->SetWindowTextW(strDPos);
				break;
			case 2:
				GetDlgItem(IDC_AxisPos2)->SetWindowTextW(strDPos);
				break;
			case 3:
				GetDlgItem(IDC_AxisPos3)->SetWindowTextW(strDPos);
				break;
			case 4:
				GetDlgItem(IDC_AxisPos4)->SetWindowTextW(strDPos);
				break;
			default:
				break;
			}
		}

	}




	CDialogEx::OnTimer(nIDEvent);
}


void CLiWiDlg::OnBnClickedStop()
{
	// TODO: 在此添加控件通知处理程序代码
	if (Data::CardIsConnect())
	{
		PMC_Rapidstop(Data::handle, 3);
	}
	else
		MessageBox(TEXT("请先连接控制器"));
}


void CLiWiDlg::OnBnClickedClear()
{
	// TODO: 在此添加控件通知处理程序代码
	if (Data::CardIsConnect())
	{
		for (size_t i = 0; i < 5; i++)
		{
			PMC_SetDpos(Data::handle, i, 0);
		}
	}
	else
		MessageBox(TEXT("请先连接控制器"));
}




BOOL CAboutDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	GetDlgItem(IDC_Mag)->SetWindowTextW(_T("1.0"));


	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
